package com.vhr.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vhr.VO.AgeCountVO;
import com.vhr.VO.ContractVO;
import com.vhr.VO.EmployeeVO;
import com.vhr.VO.ProfileVo;
import com.vhr.entities.*;
import com.vhr.form.EmployeeForm;
import com.vhr.service.IDepartmentService;
import com.vhr.service.IEmployeeService;
import com.vhr.util.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Name EmployeeController
 * @Description:
 * @Author: CHAO
 * @Date: 2019/9/26
 */
@RestController
@Api(description = "员工模块")
@CrossOrigin
public class EmployeeController {

    @Autowired
    private IEmployeeService iEmployeeService;
    @Autowired
    private IDepartmentService iDepartmentService;

    //获取所有员工信息
    @GetMapping("/emp/list")
    @ApiOperation("查看所有员工信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name="pageNum",required = false,value = "第几页",defaultValue = "1",dataType = "Integer"),
            @ApiImplicitParam(paramType = "query",name="pageSize",required = false,value = "一页有几条数据",defaultValue = "10",dataType = "Integer")
    })
    public Result empList(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                          @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize){

        PageHelper.startPage(pageNum,pageSize);
        List<EmployeeVO> list = iEmployeeService.selectAll();
        PageInfo<EmployeeVO> pageInfo=new PageInfo<>(list);
        return Result.success().add("empList", PageUtil.MyPageInfoUtil(pageInfo));
    }

    //获取单个员工信息
    @ApiOperation("根据id查看员工信息")
    @ApiImplicitParam(paramType = "query",name="id",required = true,value = "根据员工id查询员工信息",dataType = "Integer")
    @GetMapping("/emp/empInfo")
    public Result empInfo(@RequestParam("id")Integer id){
        Employee employee = new Employee();
        employee.setEmpId(id);
        Employee emp = iEmployeeService.selectEmployeeByPrimaryKey(employee);
        return Result.success().add("emp",emp);
    }

    //新增员工
    @ApiOperation("管理员：插入员工信息")
    @PostMapping("/api/emp/insertEmp")
    public Result insertEmp(@RequestBody  EmployeeForm employeeForm){
        System.out.println(employeeForm);
        employeeForm.setDeptId(iDepartmentService.selectByDeptName(employeeForm.getDeptName()));
        //插入员工信息
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeForm,employee);
        employee.setEmpStatus(0);
        employee.setEmpType(1);
        iEmployeeService.insertEmployee(employee);
        //插入合同表的信息
        Contract contract = new Contract();
        BeanUtils.copyProperties(employeeForm,contract);
        System.out.println("-------------------------------------"+employee);
        contract.setEmpId(employee.getEmpId());
        iEmployeeService.insertContract(contract);
        //插入补贴信息表
        Welfare welfare = new Welfare();
        BeanUtils.copyProperties(employeeForm,welfare);
        iEmployeeService.insertWelfare(welfare);
        System.out.println(welfare);
        employee.setWelfateId(welfare.getWelfareId());
        iEmployeeService.updateEmployeeByPrimaryKey(employee);
        return Result.success();
    }

    //删除员工
    @ApiOperation("删除员工")
    @ApiImplicitParam(paramType = "query",name="id",required = true,value = "根据员工id删除员工",dataType = "Integer")
    @DeleteMapping("/emp/deleteEmp")
    public Result deleteEmp(@RequestParam("id")Integer id){
        Employee employee = new Employee();
        employee.setEmpId(id);
        int result = iEmployeeService.delelteEmployee(employee);
        if(result==1){
            return Result.success();
        }else{
           return Result.fail();
        }
    }

    @ApiOperation("员工：个人档案")
    @GetMapping("/emp/profile")
    public Result profile(@RequestParam(value = "id",required = true)Integer id){
        Employee employee = new Employee();
        ProfileVo profileVo = new ProfileVo();
        employee.setEmpId(id);
        Employee emp = iEmployeeService.selectEmployeeByPrimaryKey(employee);
        if(emp==null){
            return Result.fail();
        }
        BeanUtils.copyProperties(emp,profileVo);
        profileVo.setContractType(iEmployeeService.selectContractByEmployee(employee).getContractType());
        return Result.success().add("profile",profileVo);
    }

    @ApiOperation("员工：合同信息")
    @GetMapping("/emp/contract")
    public Result contract(@RequestParam(value = "id",required = true)Integer id){
        Employee employee = new Employee();
        ContractVO contractVO = new ContractVO();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        employee.setEmpId(id);
        Contract contract = iEmployeeService.selectContractByEmployee(employee);
        if(contract==null){
            return Result.fail();
        }
        BeanUtils.copyProperties(contract,contractVO);
        contractVO.setContractTime(format.format(contract.getContractTime()));
        return Result.success().add("contract",contractVO);
    }

    @ApiOperation("员工：修改密码")
    @PostMapping("/emp/changePwd")
    public Result changPwd(@RequestBody Map<String,String> map){
        String oldPwd=map.get("oldPwd");
        String newPwd=map.get("newPwd");
        Integer id= Integer.parseInt(map.get("id"));
        Employee employee = new Employee();
        employee.setEmpId(id);
        Account account = iEmployeeService.selectByEmployee(employee);
        if(account.getEmpPassword().equals(oldPwd)){
            account.setEmpPassword(newPwd);
            int i = iEmployeeService.changePassword(account, employee);
            if(i==0){
               return Result.fail("修改失败");
            }else{
                return Result.success();
            }
        }else{
            return Result.fail("原密码错误");
        }
    }

    @ApiOperation("管理员：根据员工的部门id和员工的状态查询员工信息,分页默认为第一页，一页有10条数据")
    @GetMapping("/api/emp/selectByDeptAndStatusAndName")
    public Result  selectByDeptAndStatusAndName(Integer deptId,Integer status,String empName,
                                         @RequestParam(value = "pageNum",defaultValue = "1")  Integer pageNum,
                                         @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<EmployeeVO> list = iEmployeeService.selectByDeptAndStatusAndName(deptId, status,empName);
        PageInfo<EmployeeVO> pageInfo = new PageInfo<>(list);
        return Result.success().add("empList",PageUtil.MyPageInfoUtil(pageInfo));
    }

    @ApiOperation("管理员：查询用户信息给修改页面")
    @GetMapping("/api/emp/toEditPage")
    public Result ToEditPage(Integer empId){
        EmployeeForm employeeForm = iEmployeeService.selectEditPage(empId);
        return Result.success().add("empInfo",employeeForm);
    }

    @ApiOperation("管理员：修改员工信息")
    @PutMapping("/api/emp/updateEmp")
    public Result updateEmp(@RequestBody EmployeeForm employeeForm){
        System.out.println(employeeForm);
        //更新员工表的信息
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeForm,employee);
        iEmployeeService.updateEmployeeByPrimaryKey(employee);
        //更新合同表的信息
        Contract contract = new Contract();
        BeanUtils.copyProperties(employeeForm,contract);
        iEmployeeService.updateContractByEmployee(employee,contract);
        //更新补贴信息表
        Welfare welfare = new Welfare();
        BeanUtils.copyProperties(employeeForm,welfare);
        iEmployeeService.updateWelfareByEmpId(welfare,employee);
        return Result.success();
    }



    /*管理员：统计员工年龄信息:访问：http://localhost:8080/emp/countByEmpBirth
    返回年龄区间为：18-25,26-30,31-35,36-40,41-45,46-50,51-60 */
    @ApiOperation("管理员：统计员工年龄信息")
    @GetMapping("/api/countByEmpBirth")
    public Result countByEmpBirth(){
        List<AgeCountVO>  list = new ArrayList<AgeCountVO>();
        list = iEmployeeService.countByEmpBirth();

        return Result.success().add("birthList",list);
    }

    /*管理员：统计员工在职信息:访问：http://localhost:8080/emp/countByEmpStatus
     返回在职情况为：0(在职),1(待离职),2（已离职）*/
    @ApiOperation("管理员：统计员工在职信息")
    @GetMapping("/api/countByEmpStatus")
    public Result countByEmpStatus(){

        List<Integer> list = new ArrayList<Integer>();
        list = iEmployeeService.countByEmpStatus();
        return Result.success().add("statusList",list);
    }

    /*管理员：统计员工司龄:访问：http://localhost:8080/emp/countByContractDuration
    返回司龄人数情况为：1年，2年。3年，4年，5年,5年以上*/
    @ApiOperation("管理员：统计员工司龄")
    @GetMapping("/api/countByContractDuration")
    public Result countByContractDuration(){
        List<Integer> results = iEmployeeService.countByContractDuration();
        return Result.success().add("silingList",results);
    }

    /*管理员：筛选列表员工名字:访问：http://localhost:8080/emp/countByContractDuration*/
    @ApiOperation("管理员：筛选列表员工名字")
    @GetMapping("/api/searchEmployeeName")
    public Result searchEmployeeName(){
        List<Employee> results = iEmployeeService.searchEmployeeName();
        return Result.success().add("EmpList",results);
    }

    public void helloword(){
        System.out.println("hello word");
    }
}
